package com.xxl.service.impl;

import com.xxl.dao.blog.BlogMapper;
import com.xxl.dao.blog.CollectBlogMapper;
import com.xxl.dao.blog.HomBlogMapper;
import com.xxl.dao.comment.BlogCommentMapper;
import com.xxl.dto.BlogCommentDto;
import com.xxl.dto.BlogDto;
import com.xxl.dto.HomeDto;
import com.xxl.dto.UserDto;
import com.xxl.dto.vo.BlogCommentVo;
import com.xxl.service.BlogService;
import com.xxl.util.DateUtil;
import com.xxl.util.UuidUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * BlogService的实现类
 * @author xxl
 * @date 2023/3/31
 */
@Service
@Slf4j

public class BlogServiceImpl implements BlogService {
    @Resource
    BlogMapper blogMapper;
    @Resource
    BlogCommentMapper blogCommentMapper;
    @Resource
    HomBlogMapper homBlogMapper;
    @Resource
    CollectBlogMapper collectBlogMapper;

    @Override
    public List<HomeDto> queryAllBlog() {
        return homBlogMapper.queryBlogByTitle(null, null);
    }

    @Override
    public HomeDto queryBlogByUUID(String uuid) {
        return homBlogMapper.queryBLogByUuid(uuid);
    }

    @Override
    public List<HomeDto> queryAllBlogByTitle(String title) {
        return homBlogMapper.queryBlogByTitle(title, null);
    }

    @Override
    public List<HomeDto> queryAllBlogByTime() {
        List<HomeDto> homeDtos = queryAllBlog();
        //不建议使用assert这个会让服务器崩溃，如果设置全局处理Advice就会失效，这个适合在底层框架使用不适合在应用级别使用
        assert homeDtos != null : "查出的时间为空";
        ArrayList<String> dateList = new ArrayList<>();
        homeDtos.forEach(date ->{
            dateList.add(date.getDate());
        });

        return homBlogMapper.queryBlogByTitle(null,DateUtil.getDate(dateList));
    }



    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int deleteBlogByUUID(String uuid) {
        int i = deleteBlog(uuid);
        int i1 = blogMapper.deleteBlogByUUID(uuid);
        //i 和 i1的值一致且必须大于0就代表删除成功，两者数据库必须同步
        return i1 > 0 && i == i1 ? i : 0;
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int deleteBlog(String uuid) {
        return homBlogMapper.deleteBlogByUuid(uuid);
    }


    @Override
    public List<BlogDto> queryAllBLogByUsername(String username) {
        return blogMapper.queryAllBlogByUsername(username);
    }

    @Override
    public List<BlogCommentDto> queryAllCommentByUUID(String uuid) {
        return blogCommentMapper.queryAllBlogCommentByUuid(uuid);
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public <T> int postComment(T t) {
        if (t instanceof BlogCommentVo vo) {
            BlogCommentDto blogCommentDto = new BlogCommentDto(
                    UuidUtil.get15UUID(),
                    vo.getUsername(),
                    vo.getContent(),
                    vo.getBlogUuid());
            return blogCommentMapper.postComment(blogCommentDto);
        }
        return 0;
    }
    @Override
    @Transactional(rollbackFor = SQLException.class)
    public <T> int modifyBlogByUUID(String uuid, T t) {



        return blogMapper.modifyBlogByUUID(uuid, (BlogDto) t);
    }
    @Override
    @Transactional(rollbackFor = SQLException.class)
    public  int postBlog( String title, String content, String simpleContent,String username) {
        String uuid = UuidUtil.get32UUID();
        String time = DateUtil.getTime();
        log.warn("uuid="+uuid);
        log.warn("time="+time);
        HomeDto homeDto = new HomeDto();
        homeDto.setBlogUuid(uuid);
        homeDto.setDate(time);
        BlogDto blogDto = new BlogDto(username, title, content,uuid, null, simpleContent, 0, 0);
        int i = blogMapper.addBlog(blogDto);
        int i1 = homBlogMapper.addBlog(homeDto);
        return i > 0 && i == i1 ? 1 : 0;
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int deleteComment(String postCommentUsername, String commentID) {
        return blogCommentMapper.deleteByPostCommentOrMaster(postCommentUsername, commentID);
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int deleteComment(String commentID) {
        return blogCommentMapper.deleteByPostCommentOrMaster(null, commentID);
    }

    @Override
    public List<String> queryAllBlogByUsername(String username) {
        return collectBlogMapper.queryAllBlog(username);
    }

    @Override
    public List<String> findBlogFollowingByUUID(String uuid) {
        return collectBlogMapper.findCollectBlog(uuid);
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int followingBlog(String username, String uuid)  {
        assert username != null && uuid != null : "不能为空";

        //不可以收藏自己的博客
        List<BlogDto> blogDtos = queryAllBLogByUsername(username);
        List<BlogDto> blogDtos1 = blogDtos.stream().filter(b -> b.getBlogUuid().equals(uuid)).toList();

        //如果集合有说明再收藏自己的博客
        if (blogDtos1.size() > 0) {
            log.error("不可收藏自己的博客");
            return 0;
        }

        //当前用户已经收藏了博客就不允许再收藏
        List<String> ls = findBlogFollowingByUUID(uuid);

        //集合不为空表示此博客有人收藏
        if (ls != null) {
            for (String t : ls) {
                //如果收藏此博客的用户中等于这次收藏的用户表示此人已经收藏了
                if (t.equals(username)) {
                    log.error("当前用户{}已经收藏了blogID为{}", new String[]{username, uuid});
                    return 0;
                }
            }
            return collectBlogMapper.addCollectBlog(uuid, username);
        }

        return 0;
    }

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public int cancelFollowingBlog(String username, String uuid) {
        assert username != null && uuid != null : "不能为空";

        //当前用户已经取消 收藏博客就不允许再取消
        List<String> ls = findBlogFollowingByUUID(uuid);

        //集合不为空表示此博客有人收藏
        if (ls != null) {
            for (String t : ls) {
                //如果收藏此博客的用户中等于这次收藏的用户表示此人收藏了此博客，可以取消收藏
                if (t.equals(username)) {
                    return collectBlogMapper.deleteByUuid(username, uuid);
                }
            }
        }

        log.error("当前用户{}没有收藏blogID为{}的博客", new String[]{username, uuid});
        return 0;
    }
}
